package com.justgame.project.service.art.controller;

import com.justgame.project.base.annotation.ProtectAccess;
import com.justgame.project.base.annotation.VerificationCode;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.service.api.dto.ArtPageInfoDTO;
import com.justgame.project.cloud.common.entity.PageRequest;
import com.justgame.project.service.api.vo.ArtPageInfoVO;
import com.justgame.project.service.art.service.ArtPageCategoryService;
import com.justgame.project.service.art.service.ArtPageInfoService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/art/page")
@AllArgsConstructor
public class ArtPageInfoController {
    private final ArtPageCategoryService artPageCategoryService;
    private final ArtPageInfoService artPageInfoService;

    /**
     * 首页数据
     * @param dto
     * @return
     */
    @GetMapping("/index/showPage")
    public Result<?> getHomeIndexShowPage(ArtPageInfoDTO dto) {
        if (StringUtils.isNotBlank(dto.getPageCategory()) &&
                !artPageCategoryService.getAllCategoryIds().contains(dto.getPageCategory())){
            return Result.fail("无效的文章类型！");
        }
        return Result.OK(artPageInfoService.homeIndexPage(dto));
    }

    /**
     * list
     *
     * @param pageRequest
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Result<Object> getTotalArticle(PageRequest<ArtPageInfoVO> pageRequest, ArtPageInfoDTO dto) {
        return Result.OK(artPageInfoService.queryByPage(pageRequest, dto));
    }

    /**
     * 管理员删除文章
     *
     * @param pid
     * @return
     */
    @RequiresRoles("admin")
    @DeleteMapping("/admin-del")
    public Result<Object> delPageById(String pid) {
        return Result.status(artPageInfoService.deleteById(pid));
    }

    /**
     * 获取某文章的详细信息通过ID查询
     *
     * @param
     * @return
     */
    @GetMapping("/detail")
    public Result<?> getPageDetailById(String id) {
        return Result.OK(artPageInfoService.getPageDetail(id));
    }

    /**
     * 上传文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/usr/upload")
    @RequiresRoles("admin")
    @VerificationCode
    @ProtectAccess
    public Result<Object> savePage(@RequestBody ArtPageInfoDTO dto) {
        return Result.status(artPageInfoService.save(dto));
    }


}
